package com.lianniu.mgr.baseService.sysInfo.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.sysInfo.model.SysInfoVo;
import com.lianniu.mgr.baseService.sysInfo.service.ISysInfoService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;
import org.apache.log4j.Logger;
/**
 * @ClassName：SysInfoRestController
 * @Description:系统管理

 * @Since: 2016年4月8日
 * @Version:0.1
 */
@Controller
@RequestMapping("/rest")
public class SysInfoRestController {
	private final static Logger logger = Logger.getLogger(SysInfoRestController.class);
	  @Autowired
	  ISysInfoService sysInfoService;
	  /**
	   * 查询系统信息
	   * @param request
	   * @return
	   */
	  @RequestMapping(value="/sysInfoList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult sysInfoList(HttpServletRequest request,PaginationInfo paginationInfo) {
	    	logger.info("SysInfoRestController sysInfoList begin.");
		    try {
		      String sysName=StringUtil.nvl(request.getParameter("sysName"));
		      SysInfoVo sysInfoVo = new SysInfoVo();
		      if(!"".equals(sysName)){
		    	  sysInfoVo.setSysName(sysName);
		      }
		      BaseResult baseResult = sysInfoService.getSysInfoForPage(sysInfoVo,paginationInfo);
		      logger.info("sysInfoList end.");
		      logger.info("SysInfoRestController sysInfoList end.");
		      return baseResult;
		    } catch (Exception e) {
		      logger.error("SysInfoRestController sysInfoList error:" + e.getMessage());
		      logger.error("SysInfoRestController sysInfoList end.");
		      return BaseResult.exception(e.getMessage());
		    }
	  } 
	  /**
	   * 查询系统信息
	   * @param request
	   * @return
	   */
	  @RequestMapping(value="/getSysInfoById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectSysInfoById(HttpServletRequest request,PaginationInfo paginationInfo) {
	    	logger.info("SysInfoRestController getSysInfoById begin.");
		    try {
		      String id=StringUtil.nvl(request.getParameter("id"));
		      SysInfoVo sysInfoVo = new SysInfoVo();
		      sysInfoVo.setId(id);
		      BaseResult baseResult = sysInfoService.getSysInfoById(sysInfoVo);
		      logger.info("getSysInfoUpd end.");
		      logger.info("SysInfoRestController getSysInfoById end.");
		      return baseResult;
		    } catch (Exception e) {
		      logger.error("SysInfoRestController getSysInfoById error:" + e.getMessage());
		      logger.error("SysInfoRestController getSysInfoById end.");
		      return BaseResult.exception(e.getMessage());
		    }
	  } 
	  /**
	   * 
	   * sysInfoAdd:系统新增
	   * @param  @param request
	   * @return BaseResult    DOM对象
	   * @throws
	   */
	  @RequestMapping(value = {"/sysInfoAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult sysInfoAdd(HttpServletRequest request,SysInfoVo sysInfoVo) {
		  logger.info("SysInfoRestController sysInfoAdd begin.");
	      sysInfoVo.setId(StringUtil.getUUID());
	      logger.info("SysInfoRestController sysInfoAdd:"+ JSON.toJSONString(sysInfoVo));
	      try {
		      logger.info("sysInfoAdd start.");
		      BaseResult baseResult = sysInfoService.insertSysInfo(sysInfoVo);
		      logger.info("sysInfoAdd baseResult:" + JSON.toJSONString(baseResult));
		      logger.info("sysInfoAdd end.");

	      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
	        logger.info("SysInfoRestController sysInfoAdd end.");
	        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
	      }
	      
	      int count= (int) baseResult.getData();

	      if (count<= 0) {
	        logger.info("MenuController sysInfoAdd end.");
	        return BaseResult.fail("0002", "新增失败");
	      }

	      logger.info("MenuController sysInfoAdd end.");
	      return BaseResult.success(count);

	    } catch (Exception e) {
	      logger.error("MenuController sysInfoAdd error:" + e.getMessage());
	      logger.info("MenuController sysInfoAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  /**
	   * 
	   * sysInfoAdd:系统修改
	   * @param  @param request
	   * @return BaseResult    DOM对象
	   * @throws
	   */
	  @RequestMapping(value = {"/sysInfoUpd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult sysInfoUpd(HttpServletRequest request,SysInfoVo sysInfoVo) {
		  logger.info("SysInfoRestController sysInfoUpd begin.");
	      logger.info("SysInfoRestController sysInfoUpd:"+ JSON.toJSONString(sysInfoVo));
	      try {
		      logger.info("sysInfoUpd start.");
		      BaseResult baseResult = sysInfoService.editSysInfoById(sysInfoVo);
		      logger.info("sysInfoUpd baseResult:" + JSON.toJSONString(baseResult));
		      logger.info("sysInfoUpd end.");

	      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
	        logger.info("SysInfoRestController sysInfoUpd end.");
	        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
	      }
	      
	      int count= (int) baseResult.getData();

	      if (count<= 0) {
	        logger.info("MenuController sysInfoUpd end.");
	        return BaseResult.fail("0002", "更新失败");
	      }

	      logger.info("MenuController sysInfoUpd end.");
	      return BaseResult.success(count);

	    } catch (Exception e) {
	      logger.error("MenuController sysInfoUpd error:" + e.getMessage());
	      logger.info("MenuController sysInfoUpd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  /**
	   * 
	   * sysInfoDel:系统新增
	   * @param  @param request
	   * @return BaseResult    DOM对象
	   * @throws
	   */
	  @RequestMapping(value = {"/sysInfoDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult sysInfoDel(HttpServletRequest request) {
		  logger.info("SysInfoRestController sysInfoDel begin.");
	      String ids=StringUtil.nvl(request.getParameter("ids"));
	      try {
	    	  String[] idArr = ids.split(",");
	    	  List<String> list = new ArrayList<String>();
	    	  for (String  s : idArr) {
	    		  list.add(s);
	    	  }
	    	  logger.info("SysInfoRestController sysInfoDel:"+ JSON.toJSONString(list));
		      logger.info("sysInfoDel start.");
		      BaseResult baseResult = sysInfoService.deleteSysInfoById(list);
		      logger.info("sysInfoDel baseResult:" + JSON.toJSONString(baseResult));
		      logger.info("sysInfoDel end.");

	      if (baseResult.getRetCode().equals("0003")){
	    	  logger.info("SysInfoRestController sysInfoDel end.");
	    	  return BaseResult.fail("0003", "已有菜单，无法删除！");
	      }
	      else if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
	        logger.info("SysInfoRestController sysInfoDel end.");
	        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
	      }
	      int count= (int) baseResult.getData();
	      if (count<= 0) {
	        logger.info("SysInfoRestController sysInfoDel end.");
	        return BaseResult.fail("0002", "删除失败");
	      }
	      logger.info("SysInfoRestController sysInfoDel end.");
	      return BaseResult.success(count);
	    } catch (Exception e) {
	      logger.error("SysInfoRestController sysInfoDel error:" + e.getMessage());
	      logger.info("SysInfoRestController sysInfoDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
}
